using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeSharp;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace FelMaster.Classes.Druid
{
    class FeralDruid : TalentBase
    {
        public override string Name
        {
            get { return "Feral Druid V2.1"; }
        }

        public override string KeySpell
        {
            get { return "Mangle"; }
        }
        
        public bool IsBehind(WoWUnit target)
        {
            return WoWMathHelper.IsBehind(StyxWoW.Me.Location, target.Location, target.Rotation, (float)Math.PI*5/6);
        }
        
        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                    SC.CastSpell("Berserking", a => true, "Berserking"),
                    #region Bear
                    new Decorator(
                        ret => SC.PlayerHasBuff("Bear Form"),
                        new PrioritySelector(
                            SC.CastSpell("Lacerate", a => (SC.TargetDebuffTimeLeft("Lacerate") < 2) && !SC.CanCast("Pulverize"), "Lacerate"),
                            SC.CastSpell("Thrash", a => SC.PlayerHasBuff("Berserk"), "Thrash"),
                            SC.CastSpell("Enrage", a => !SC.CanCast("Berserk"), "Enrage"),
                            SC.CastSpell("Maul", a => Me.RagePercent > 80, "Maul"),
                            SC.CastSpell("Mangle(Bear Form)", a => true, "Mangle"),
                            SC.CastSpell("Thrash", a => true, "Thrash"),
                            SC.CastSpell("Pulverize", a => !SC.PlayerHasBuff("Pulverize") && (SC.TargetCountDebuff("Lacerate") > 2), "Pulverize"),
                            SC.CastSpell("Berserk", a => SC.PlayerHasBuff("Pulverize"), "Berserk"),
                            SC.CastAreaSpell("Swipe(Bear Form)", 8, false, 3, 0.0, 0.0, ret => true, "Swipe"),
                            SC.CastSpell("Lacerate", a => SC.TargetCountDebuff("Lacerate") < 3, "Lacerate"),
                            SC.CastSpell("Faerie Fire (Feral)", a => true, "Faerie Fire (Feral)"),
                            SC.CastSpell("Swipe(Bear Form)", a => true, "Swipe"),
                            SC.CastSpell("Lacerate", a => true, "Lacerate")
                        )
                    ),
                    #endregion
                    #region Cat
                    new Decorator(
                        ret => SC.PlayerHasBuff("Cat Form"),
                        new PrioritySelector(
                            SC.CastSpell("Tiger's Fury", a => Me.CurrentEnergy < 35, "Tigers Fury"),
                            SC.CastSpell("Berserk", a => SC.PlayerHasBuff("Tiger's Fury"), "Berserk"),
                            SC.CastSpell("Berserking", a => SC.PlayerHasBuff("Tiger's Fury"), "Berserking"),
                            SC.CastAreaSpell("Swipe(Cat Form)", 8, false, 3, 0.0, 0.0, ret => SC.PlayerHasBuff("Savage Roar"), "Swipe"),
                            SC.CastSpell("Mangle(Cat Form)", a => SC.TargetDebuffTimeLeft("Mangle") < 2, "Mangle"),
                            SC.RunMacroText("/Cast Ravage(Cat Form)", a => SC.PlayerHasBuff("Stampede"), "Ravage"),
                            SC.CastSpell("Ferocious Bite", a => Me.ComboPoints >= 1 && SC.TargetDebuffTimeLeft("Rip") < 2 && SC.TargetHasDebuff("Rip") && Me.CurrentTarget.HealthPercent < 25, "Ferocious Bite"),
                            SC.CastSpell("Ferocious Bite", a => Me.ComboPoints == 5 && SC.TargetHasDebuff("Rip") && Me.CurrentTarget.HealthPercent < 25, "Ferocious Bite"),
                            SC.CastSpell("Rip", a => Me.ComboPoints == 5 && SC.TargetDebuffTimeLeft("Rip") < 1, "Rip"),
                            SC.CastSpell("Ferocious Bite", a => Me.ComboPoints == 5 && SC.TargetDebuffTimeLeft("Rip") > 5 && SC.PlayerBuffTimeLeft("Savage Roar") > 3 && SC.PlayerHasBuff("Berserk"), "Ferocious Bite"),
                            SC.CastSpell("Rake", a => SC.TargetDebuffTimeLeft("Rake") < 9 && SC.PlayerHasBuff("Tiger's Fury"), "Rake"),
                            SC.CastDebuff("Rake", a => true, "Rake"),
                            SC.CastSpell("Mangle(Cat Form)", a => !IsBehind(Me.CurrentTarget) && SC.PlayerHasBuff("Clearcasting"), "Mangle"),
                            SC.CastSpell("Shred", a => SC.PlayerHasBuff("Clearcasting"), "Shred"),
                            SC.CastSpell("Savage Roar", a => SC.PlayerBuffTimeLeft("Savage Roar") < 1 && Me.ComboPoints >= 1, "Savage Roar"),
                            SC.CastSpell("Ferocious Bite", a => Me.ComboPoints == 5 && SC.TargetDebuffTimeLeft("Rip") > 14 && SC.PlayerBuffTimeLeft("Savage Roar") > 10, "Ferocious Bite"),
                            SC.RunMacroText("/Cast Ravage(Cat Form)", a => SC.PlayerHasBuff("Stampede") && !SC.PlayerHasBuff("Clearcasting") && SC.PlayerHasBuff("Tiger's Fury"), "Ravage"),
                            SC.CastSpell("Mangle(Cat Form)", a => !IsBehind(Me.CurrentTarget) && (Me.ComboPoints < 5 || Me.CurrentEnergy > 80), "Mangle"),
                            SC.CastSpell("Shred", a => (Me.ComboPoints < 5 || Me.CurrentEnergy > 60 || SC.PlayerHasBuff("Berserk")), "Shred"),
                            SC.CastDebuff("Faerie Fire (Feral)", a => true, "Faerie Fire (Feral)")
                        )
                    ) 
                    #endregion   
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Mark of the Wild", a => !SC.PlayerHasBuff("Blessing of Kings") && !Me.Combat && SC.PlayerBuffTimeLeft("Mark of the Wild") < 900, "Mark of the Wild")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}